package com.anjoyo.xgh.musicshare.api;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.anjoyo.xgh.musicshare.application.SongApplication;
import com.anjoyo.xgh.musicshare.db.MusicDBhelper;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.MusicSpf;
import com.anjoyo.xgh.musicshare.util.StringUtil;

public class CollectApi {

	/**
	 * 添加到网络收藏
	 * 
	 * @param con
	 * @param song
	 */
	public void clodesong(final Context con, final Song song) {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams ajaxParams = new AjaxParams();
		ajaxParams.put("name", song.getName());
		ajaxParams.put("singer", song.getArtist());
		if (!StringUtil.isnull(song.getAblum())) {
			ajaxParams.put("album", song.getAblum());
		}
		ajaxParams.put("song_net_url", song.getNet_url());
		ajaxParams.put("album_url", song.getImage_url());
		ajaxParams.put("musicid", song.getMusicid() + "");
		ajaxParams.put("userid", MusicSpf.getloginUser(con).getId() + "");
		ajaxParams.put("username", MusicSpf.getloginUser(con).getName());

		finalHttp.get(Constant.CLODE_SONG, ajaxParams,
				new AjaxCallBack<Object>() {
					@Override
					public void onSuccess(Object t) {
						super.onSuccess(t);
						try {
							JSONObject object = new JSONObject(t.toString());
							if (object.getString("code").equals("200")) {
								MusicDBhelper dBhelper = new MusicDBhelper(con);
								dBhelper.saveMusic(song, true, false);
								Toast.makeText(con, object.getString("msg"), 0)
										.show();
							} else {
								Toast.makeText(con, "收藏失败", 0).show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	/**
	 * 得到网络收藏列表 ，实现同步功能
	 * @param con
	 * @param handler
	 */
	public void getclodelist(final Context con,final Handler handler) {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams ajaxParams = new AjaxParams();
		ajaxParams.put("userid", MusicSpf.getloginUser(con).getId() + "");
		ajaxParams.put("username", MusicSpf.getloginUser(con).getName());

		finalHttp.get(Constant.GET_CLODESONGLIST, ajaxParams,
				new AjaxCallBack<Object>() {
					@Override
					public void onSuccess(Object t) {
						super.onSuccess(t);
						try {
							JSONObject object2 = new JSONObject(t.toString());
							if (object2.getString("code").equals("200")) {
								SongApplication.getInstance().getDBhelper()
										.deletAllFavsong();
								JSONArray array = object2.getJSONArray("msg");
								JSONObject object = new JSONObject();
								Song song = null;
								for (int i = 0; i < array.length(); i++) {
									object = array.getJSONObject(i);
									song = new Song();
									song.setMusicid(object.getInt("musicid"));
									song.setAblum(object.getString("album"));
									song.setName(object.getString("name"));
									song.setImage_url(object
											.getString("album_url"));
									song.setArtist(object.getString("singer"));
									song.setNet_url(object
											.getString("song_net_url"));
									song.setIsnetMusic(true);
									SongApplication.getInstance().getDBhelper()
											.saveMusic(song, true, false);
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						handler.sendEmptyMessage(1);
					}
				});
	}
}
